<script setup>
import { getAppointmentList, deleteAppointment } from '@/api/appointment'
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const appointmentList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)


onMounted(async () => {
    await fetchAppointments()
})

const fetchAppointments = async () => {
    try {
        const res = await getAppointmentList({ page: currentPage.value, size: pageSize.value })
        appointmentList.value = res.data
        total.value = res.data.length
    } catch (error) {
        console.error("Failed to fetch appointments:", error)
    }
}
const handleDelete = async (row) => {
    try {
        ElMessageBox.confirm('确定要删除这个预约吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
            await deleteAppointment(row.appointmentId)
            ElMessage.success("删除成功")
            await fetchAppointments()
        }).catch(() => {
            ElMessage.info("已取消删除")
        })
    } catch (error) {
        console.error("Failed to delete appointment:", error)
    }
}
const handlePageChange = (newPage) => {
    currentPage.value = newPage
    fetchAppointments()
}

</script>
<template>
    <div>
        <el-table :data="appointmentList" style="width: 100%">
            <el-table-column label="序号" width="100">
                <template #default="scope">
                    {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column prop="patientName" label="患者姓名" width="200" />
            <el-table-column prop="doctorName" label="医生姓名" width="200" />
            <el-table-column prop="clinicName" label="科室名称" width="200" />
            <el-table-column prop="appointmentTime" label="预约时间" width="200" />
            <el-table-column prop="status" label="状态" width="200" />
            <el-table-column prop="action" label="操作" width="200">
                <template #default="scope">
                    <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="total, prev, pager, next, jumper"
      @current-change="handlePageChange"
    />
    </div>
</template>

<style scoped></style>
